package com.ny.controller;

import com.ny.entity.Classrooms;
import com.ny.entity.Reservations;
import com.ny.entity.Users;
import com.ny.service.ClassroomService;
import com.ny.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.ParseException;
import java.text.SimpleDateFormat;

@Controller



public class ApplyController {
    @Autowired
    private ClassroomService classroomService;
    @RequestMapping("/apply")
    public String apply(){
        return "apply";
    }
    // 申请界面
    @RequestMapping("/submitApply")
    public String submitApply(@RequestParam int classroomId, @RequestParam int userId, @RequestParam String startTime, @RequestParam String endTime, @RequestParam String reason)
    throws ParseException
    {
        classroomService.updateClassroomStatusById(classroomId,"申请中");
        Reservations reservations = new Reservations();
        Users users = new Users();
        users.setUserId(userId);
        reservations.setUser(users);
        Classrooms classrooms = new Classrooms();
        classrooms.setClassroomId(classroomId);
        reservations.setClassroom(classrooms);
        SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd'T'HH:mm");
        reservations.setStartDatetime(sdf.parse(startTime));
        reservations.setEndDatetime(sdf.parse(endTime));
        reservations.setStatus("申请中");
        reservations.setReason(reason);

        return "redirect:/classroomsList";
    }
}